package android;



import hibernate.Parkstab;
import hibernate.ParkstabDAO;

import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetParkDetailServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetParkDetailServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		System.out.println("get connection in getparkDetail");
		JSONArray jsonArray=null;


		try {
			jsonArray=new JSONArray(request.getParameter("jsonString"));


		} catch (JSONException e1) {

			System.out.println("GetParkDetailServlet: json parse faile");
			e1.printStackTrace();
		}	

		System.out.println(String.format("get parkid= %s \n",jsonArray.toString()));


		List<Parkstab> parkstab=new LinkedList<Parkstab>();
		ParkstabDAO parkstabDAO=new ParkstabDAO();

		parkstabDAO.getSession().beginTransaction();
		try
		{
			String hql="from Parkstab as d where d.uuid = :parkid";
			for(int i=0;i<jsonArray.length();i++)
			{

				Query result=parkstabDAO.getSession().createQuery(hql);
				result.setInteger("parkid",Integer.valueOf(jsonArray.getInt(i)));
				parkstab.addAll(result.list());
			}

		}
		catch(HibernateException e)
		{
			System.out.println("hibernate error in GetParkDetailServlet");
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			parkstabDAO.getSession().getTransaction().commit();
			parkstabDAO.getSession().close();

		}


		JSONObject resultJson=new JSONObject();
		JSONArray resultArray=new JSONArray();

		try {

			for(int i=0;i<jsonArray.length();i++)
			{
				Parkstab tempPark=parkstab.get(i);
				JSONObject tempJsonObj=new JSONObject();
				tempJsonObj.put("uuid", tempPark.getUuid());
				tempJsonObj.put("name", tempPark.getName());
				tempJsonObj.put("totalSpace", tempPark.getTotalSpace().toString());
				tempJsonObj.put("emptySpace", tempPark.getEmptySpace().toString());
				tempJsonObj.put("description", tempPark.getDescription());
				tempJsonObj.put("address", tempPark.getDescription());
				tempJsonObj.put("longitude",tempPark.getLongitude());
				tempJsonObj.put("latitude", tempPark.getLatitude());
				tempJsonObj.put("price", tempPark.getPrice());
				tempJsonObj.put("telNumber", tempPark.getTelNumber());
				tempJsonObj.put("maxOrderTime",tempPark.getMaxOrderTime());
				resultArray.put(tempJsonObj);
			}
			
			resultJson.put("parksArray", resultArray);
		} catch (JSONException e) {

			e.printStackTrace();
		}

		
		OutputStream out=response.getOutputStream();
		out.write(resultJson.toString().getBytes("utf-8"));
		out.flush();
		out.close();
		System.out.println(resultJson.toString()+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

	}




}
